//
//  HNSH_GoodsDetailScrollInfoCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/25.
//

#import "HNSH_GoodsDetailScrollInfoCell.h"

//view
#import "SDCycleScrollView.h"
#import "KKImageBrowser.h"

#import "CJJTimerView.h"

@interface HNSH_GoodsDetailScrollInfoCell ()<SDCycleScrollViewDelegate>

/** 轮播图 */
@property (nonatomic, strong)SDCycleScrollView * cycleScrollView;

/* infoBgView */
@property (nonatomic, strong) UIView *infoBgView;
/** 名字 */
@property (nonatomic, strong)UILabel * nameLabel;
/** 送积分*/
//@property (nonatomic, strong)UILabel * sendJfLabel;
///** 送积分背景 */
//@property (nonatomic, strong)UIImageView * sendJfBgImgV;
/** 价格 */
@property (nonatomic, strong)UILabel * priceLabel;
/** 已售 */
@property (nonatomic, strong)UILabel * salesLabel;

/* msBgView */
@property (nonatomic, strong) UIView *msBgView;
@property (nonatomic, strong) UILabel *msTitleLabel;
@property (nonatomic, strong) CJJTimerView *timer;

@end

@implementation HNSH_GoodsDetailScrollInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor appWhiteLabelColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_GoodsDetailModel *)model {
    _model = model;
    self.cycleScrollView.imageURLStringsGroup = [self getUrlsArray];
    
    self.nameLabel.text = model.name;
    
    self.salesLabel.text = [NSString stringWithFormat:@"原价：%@",model.sales_count];
    //中划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:self.salesLabel.text attributes:attribtDic];
    // 赋值
    self.salesLabel.attributedText = attribtStr;
    
    
    self.priceLabel.text = [NSString stringWithFormat:@"¥%.2f",[model.price doubleValue]];
    self.priceLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:self.priceLabel.text SelectContent:@"¥" CommonFont:self.priceLabel.font SpeFont:[UIFont systemFontOfSize:14] Color:self.priceLabel.textColor];
    
//    self.sendJfLabel.hidden = [model.reward_amount doubleValue] <= 0;
//    
//    self.sendJfLabel.text = [NSString stringWithFormat:@" 赠购物值%.2f ",[model.reward_amount doubleValue]];
//    if ([model.reward_integral doubleValue] > 0) {
//        self.sendJfLabel.hidden = NO;
//        self.sendJfLabel.text = [NSString stringWithFormat:@" 赠冻结能量%.2f ",[model.reward_integral doubleValue]];
//    }
    
    
    self.msBgView.hidden = self.msTitleLabel.hidden = self.timer.hidden = !([model.seckill_info.start_time isNotBlank] && [model.seckill_info.uid integerValue] > 0);
    if ([NSString getNowTimeTimestamp].integerValue/1000 <= [model.seckill_info.start_time integerValue]) {//活动未开始
        self.msBgView.backgroundColor = [UIColor appGreenColor];
        self.msTitleLabel.text = @"距开抢：";
        [self.timer resetTimerWithlastTime:model.seckill_info.start_time];
        [self.timer startTimer];
        [self.msTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(-54);
        }];
    } else if ([NSString getNowTimeTimestamp].integerValue/1000 <= [model.seckill_info.end_time integerValue]) {//开始未结束
        self.msBgView.backgroundColor = [UIColor appGreenColor];
        self.msTitleLabel.text = @"还剩：";
        [self.timer resetTimerWithlastTime:model.seckill_info.end_time];
        [self.timer startTimer];
        [self.msTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(-54);
        }];
    } else {
        self.msBgView.backgroundColor = [UIColor appGrayLabelColor];
        self.msTitleLabel.text = @"已结束";
        [self.timer stopTimer];
        [self.msTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
        }];
    }
}

- (NSArray *)getUrlsArray{
    NSMutableArray * arr = [NSMutableArray new];
    for (HNSH_GoodsDetailImgInfoModel * m in self.model.banner_list) {
        [arr addObject:m.content];
    }
    return arr.copy;
}

- (void)configUI {
    [self.contentView addSubview:self.cycleScrollView];
    
    //秒杀倒计时
    [self.contentView addSubview:self.msBgView];
    [self.msBgView addSubview:self.msTitleLabel];
    [self.msBgView addSubview:self.timer];
    
    [self.contentView addSubview:self.infoBgView];
    [self.contentView addSubview:self.priceLabel];
//    [self.contentView addSubview:self.sendJfBgImgV];
//    [self.contentView addSubview:self.sendJfLabel];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.salesLabel];
    
//    [self.contentView addSubview:self.xfedBgView];
//    [self.xfedBgView addSubview:self.xfedTitleLabel];
//    [self.xfedBgView addSubview:self.xfedTimeLabel];
    
    
    
//    [self.sendJfLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.priceLabel.mas_centerY);
//        make.left.equalTo(self.priceLabel.mas_right).offset(12);
//        make.height.mas_equalTo(16);
//    }];
//    [self.sendJfBgImgV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.sendJfLabel.mas_centerY);
//        make.left.mas_equalTo(self.sendJfLabel.mas_left).offset(-6);
//        make.right.mas_equalTo(self.sendJfLabel.mas_right).offset(6);
//        make.height.mas_equalTo(24);
//    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(kScreenWidth+16);
        make.bottom.mas_equalTo(-50);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-16);
        make.left.mas_equalTo(16);
        make.height.mas_equalTo(26);
    }];
    
    [self.salesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLabel.mas_right).offset(8);
        make.height.mas_equalTo(20);
        make.bottom.mas_equalTo(-16);
    }];
    
    [self.infoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(kScreenWidth);
    }];
    
    
    self.msTitleLabel.text = @"距结束：";
    [self.msTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(-54);
        make.top.mas_equalTo(11);
        make.height.mas_equalTo(25);
    }];
    
    [self.timer configureLayout:^(CGFloat timerWidth, CGFloat timerHeight) {
        [self.timer mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.msTitleLabel.mas_right);
            make.centerY.mas_equalTo(self.msTitleLabel);
            make.size.mas_equalTo(CGSizeMake(timerWidth, timerHeight));
        }];
    }];
    
//    [self.xfedBgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.right.mas_equalTo(-16);
//        make.left.mas_equalTo(16);
//        make.height.mas_equalTo(34);
//    }];
//    
//    [self.xfedTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.xfedBgView.mas_centerY);
//        make.left.mas_equalTo(12);
//        make.height.mas_equalTo(25);
//    }];
//    
//    [self.xfedTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.xfedBgView.mas_centerY);
//        make.right.mas_equalTo(-12);
//        make.height.mas_equalTo(25);
//    }];
        
}

//MARK: SDCycleScrollViewDelegate Methods
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSMutableArray *images = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.model.banner_list.count; i ++) {
        HNSH_GoodsDetailImgInfoModel *info = self.model.banner_list[i];
        KKImageBrowserModel *model = [[KKImageBrowserModel alloc] initWithURL:[NSURL URLWithString:info.content] toView:kSTWindow];
        [images addObject:model];
    }
    KKImageBrowser *vc = [[KKImageBrowser alloc] init];
    vc.images = images;
    vc.index = index;
    [[UIViewControllerHelper findCurrentShowingViewController] presentViewController:vc animated:NO completion:nil];
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
//    self.indexLabel.text = [NSString stringWithFormat:@"%ld/%ld", index + 1, self.detailModel.cover_image_list.count];
}


#pragma mark lazy
- (SDCycleScrollView *)cycleScrollView{
    if (!_cycleScrollView) {
        _cycleScrollView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth)];
        _cycleScrollView.delegate = self;
        _cycleScrollView.placeholderImage = [UIImage imageNamed:@"icon_place"];
        _cycleScrollView.autoScrollTimeInterval = 3.0;
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
        _cycleScrollView.titleLabelBackgroundColor = [UIColor clearColor];
        _cycleScrollView.backgroundColor = [UIColor grayColor];
    }
    return _cycleScrollView;
}

- (UIView *)infoBgView {
    if (!_infoBgView) {
        _infoBgView = [[UIView alloc]init];
        _infoBgView.backgroundColor = [UIColor appWhiteLabelColor];
//        _infoBgView.layer.cornerRadius = 20.0f;
//        _infoBgView.layer.masksToBounds = YES;
    }
    return _infoBgView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = [UIColor appBlackLabelColor];
        _nameLabel.font = [UIFont boldSystemFontOfSize:18];
        _nameLabel.text = @"";
        _nameLabel.numberOfLines = 0;
    }
    return _nameLabel;
}

//- (UILabel *)sendJfLabel{
//    if (!_sendJfLabel) {
//        _sendJfLabel = [UILabel new];
//        _sendJfLabel.text = @"";
//        _sendJfLabel.textColor = [UIColor appOrangeLabelColor];
//        _sendJfLabel.font = [UIFont systemFontOfSize:11];
//        _sendJfLabel.textAlignment = NSTextAlignmentCenter;
//        _sendJfLabel.layer.cornerRadius = 3.0f;
//        _sendJfLabel.layer.masksToBounds = YES;
//        _sendJfLabel.layer.borderColor = [[UIColor appOrangeLabelColor] CGColor];
//        _sendJfLabel.layer.borderWidth = 1.0f;
//    }
//    return _sendJfLabel;
//}

//- (UIImageView *)sendJfBgImgV {
//    if (!_sendJfBgImgV) {
//        _sendJfBgImgV = [UIImageView new];
//        _sendJfBgImgV.image = [UIImage imageNamed:@"home_goods_cell_jf_bg"];
//    }
//    return _sendJfBgImgV;
//}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.text = @"";
        _priceLabel.textColor = [UIColor appGreenColor];
        _priceLabel.font = [UIFont boldSystemFontOfSize:24];
    }
    return _priceLabel;
}

- (UILabel *)salesLabel {
    if (!_salesLabel) {
        _salesLabel = [UILabel new];
        _salesLabel.text = @"原价：0";
        _salesLabel.textColor = [UIColor appGrayLabelColor];
        _salesLabel.font = [UIFont systemFontOfSize:12];
    }
    return _salesLabel;
}

- (UIView *)msBgView {
    if (!_msBgView) {
        _msBgView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenWidth-86, kScreenWidth, 86)];
        _msBgView.backgroundColor = [UIColor appGreenColor];
        [self clipCornerWithView:_msBgView cornerWidth:20 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
    }
    return _msBgView;
}

- (UILabel *)msTitleLabel {
    if (!_msTitleLabel) {
        _msTitleLabel = [UILabel new];
        _msTitleLabel.text = @"";
        _msTitleLabel.textColor = [UIColor appWhiteLabelColor];
        _msTitleLabel.font = [UIFont systemFontOfSize:18];
    }
    return _msTitleLabel;
}

- (CJJTimerView *)timer{
    if(!_timer){
        CJJTimerViewConfiguration *configuration = [CJJTimerViewConfiguration configureTimerView];
        configuration.autoStart(NO)
        .viewWidth(22)
        .viewHeight(25)
        .lastTime([NSString stringWithFormat:@"%ld",[NSString getNowTimeTimestamp].integerValue/1000+15*60*60])
        .cornerRadius(3)
        .backgroundColor([UIColor clearColor])
        .textLabelFont([UIFont systemFontOfSize:16])
        .textLabelColor([UIColor appWhiteLabelColor])
        .colonLabelFont([UIFont systemFontOfSize:18])
        .colonLabelColor([UIColor appWhiteLabelColor]);
        
        _timer = [CJJTimerView timerViewWithConfiguration:configuration];
    }
    return _timer;
}


//- (UIView *)xfedBgView {
//    if (!_xfedBgView) {
//        _xfedBgView = [[UIView alloc]init];
//        _xfedBgView.backgroundColor = [UIColor colorWithHexString:@"#FFF3F2"];
//        _xfedBgView.layer.cornerRadius = 4.0f;
//        _xfedBgView.layer.masksToBounds = YES;
//    }
//    return _xfedBgView;
//}
//
//- (UILabel *)xfedTitleLabel {
//    if (!_xfedTitleLabel) {
//        _xfedTitleLabel = [UILabel new];
//        _xfedTitleLabel.text = @"消费额度：0.00";
//        _xfedTitleLabel.textColor = [UIColor appGreenColor];
//        _xfedTitleLabel.font = [UIFont systemFontOfSize:14];
//    }
//    return _xfedTitleLabel;
//}
//
//- (UILabel *)xfedTimeLabel {
//    if (!_xfedTimeLabel) {
//        _xfedTimeLabel = [UILabel new];
//        _xfedTimeLabel.text = @"额度更新时间：";
//        _xfedTimeLabel.textColor = [UIColor appGrayLabelColor];
//        _xfedTimeLabel.font = [UIFont systemFontOfSize:11];
//    }
//    return _xfedTimeLabel;
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
